package im.dhc.gitdiff.common;

import im.dhc.gitdiff.util.PathUtil;

import java.io.BufferedWriter;
import java.io.IOException;

/**
 * @author songhaichao
 * @create 2020-06-09-9:57
 **/
public class AddDiffHandler extends AbstractDiffHandler {

    public AddDiffHandler(String oldPath,String newPath,BufferedWriter backupbw,BufferedWriter updatebw,BufferedWriter recoverbw) throws Exception {
        super(oldPath,newPath,backupbw,updatebw,recoverbw);
        if(PathUtil.isJavaFile(newPath)){
            this.oldPath = this.newPath;
        }
    }

    public void backupHandle() throws IOException {
        //如果是新模块会有错误？
        if(PathUtil.isJarFile(this.newPath)){
            this.backupbw.write(this.backUp());
            this.backupbw.newLine();
        }
    }

    public void updateHandle() throws IOException {
        this.updatebw.write(update());
        this.updatebw.newLine();
    }

    public void recoverHandle() throws IOException {
        if(PathUtil.isJarFile(this.newPath)){
            this.recoverbw.write(recover());
        }else {
            this.recoverbw.write(delete(this.newPath));
        }
        this.recoverbw.newLine();
    }

    public void copyFileHandle() throws IOException {
        copyFile();
    }
}
